<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-09-05
 * Time: 12:12
 */

namespace App\Http\Middleware;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserManager;
use App\Models\UserManagerLog;
use Closure;
use Illuminate\Support\Facades\Auth;

class AgentLogin extends Controller
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $accessToken = $request->header("accessToken");
        if(!$accessToken){
            $accessToken = $request->accessToken;
        }
        $agent = User::where('accessToken', $accessToken)->first();

        if (!$agent) {
            return $this->apiResponse('', config('errorCode.PERMISSION_DENY'));
        }
        $appType = $request->header("appType");
        $pageSize = $request->pageSize ? $request->pageSize : 20;
        $userId = $agent->userId;
        $request->USERID = $userId;
        $request->pageSize = $pageSize;
        $request->appType = $appType;
        return $next($request);
    }
}
